﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;

namespace TIMA
{
    public static class GenericExtensions
    {
        public static Double SecondsPast(this DateTime date)
        {
            return DateTime.UtcNow.Subtract(date).TotalSeconds;
        }
 
  
        public static Bitmap ToBitmap(this byte[] bytes)
        {
            Bitmap bitmap = null;
            using (MemoryStream ms = new MemoryStream(bytes))
            {
                bitmap = (Bitmap)Image.FromStream(ms);
            }
            return bitmap;
        }

        public static decimal ToDecimalNN(this object value)
        {
            decimal result = -1;

            if (value == DBNull.Value)
            {
            }
            else if (value == null)
            {
            }
            else
            {
                try
                {
                    result = Convert.ToDecimal(value, CultureInfo.CurrentCulture);

                    //CultureInfo ci = new CultureInfo("en-US");
                    //result = Convert.ToDecimal(value, ci);
                }
                catch
                {

                }
            }
            return result;
        }

        public static decimal ToDecimalZero(this object value)
        {
            decimal result = 0;

            if (value == DBNull.Value)
            {
            }
            else if (value == null)
            {
            }
            else
            {
                try
                {
                    result = Convert.ToDecimal(value, CultureInfo.CurrentCulture);

                    //CultureInfo ci = new CultureInfo("en-US");
                    //result = Convert.ToDecimal(value, ci);
                }
                catch
                {

                }
            }
            return result;
        }

        public static double ToDoubleZero(this object value)
        {
            double result = 0;

            if (value == DBNull.Value)
            {
            }
            else if (value == null)
            {
            }
            else
            {
                try
                {
                    result = (double)value;
                }
                catch
                {

                }
            }
            return result;
        }

        public static int ToIntNN(this object value)
        {
            int result = -1;

            if (value == DBNull.Value)
            {
            }
            else if (value == null || value.ToString() == "")
            {
            }
            else
            {
                try
                {
                    result = Convert.ToInt32(value);
                }
                catch
                {
                }
            }
            return result;
        }

        public static object ToIntDBNull(this object value)
        {
            if (value == DBNull.Value)
            {
            }
            else if (value == null || value.ToString() == "")
            {
            }
            else
            {
                try
                {
                    int result = Convert.ToInt32(value);
                    return result;
                }
                catch
                {
                }
            }
            return DBNull.Value;
        }

        public static int ToIntZero(this object value)
        {
            int result = 0;

            if (value == DBNull.Value)
            {
            }
            else if (value == null || value.ToString() == "")
            {
            }
            else
            {
                try
                {
                    result = Convert.ToInt32(value);
                }
                catch
                {
                }
            }
            return result;
        }

        public static long ToBigIntNN(this object value)
        {
            long result = -1;

            if (value == DBNull.Value)
            {
            }
            else if (value == null)
            {
            }
            else
            {
                try
                {
                    result = Convert.ToInt64(value);
                }
                catch
                {
                }
            }
            return result;
        }

        public static bool ToBool(this object value)
        {
            bool result = false;

            if (value == DBNull.Value)
            {
            }
            else if (value == null)
            {
            }
            else
            {
                try
                {
                    result = Convert.ToBoolean(value);
                }
                catch
                {
                }
            }
            return result;
        }


        public static string ToStringNN(this object value)
        {
            string result = "";

            if (value == DBNull.Value)
            {
            }
            else if (value == null)
            {
            }
            else
            {
                result = value.ToString().Trim();
            }
            return result;
        }

        public static DateTime ToDate(this object value)
        {
            DateTime result = new DateTime(2000, 1, 1);

            if (value == DBNull.Value)
            {
            }
            else if (value == null)
            {
            }
            else
            {
                DateTime.TryParse(value.ToString(), out result);
            }
            return result;
        }

        public static bool ToBool(this DefaultBoolean value)
        {
            return value == DefaultBoolean.True ? true : false;
        }

        public static DefaultBoolean Toggle(this DefaultBoolean value)
        {

            if (value == DefaultBoolean.True)
                return DefaultBoolean.False;
            else if (value == DefaultBoolean.False)
                return DefaultBoolean.True;
            else // TODO : bu kısım sorun çıkarabilir
                return DefaultBoolean.Default;
        }

        public static string RemoveBetween(this string source, char begin, char end)
        {
            Regex regex = new Regex(string.Format("\\{0}.*?\\{1}", begin, end));
            return regex.Replace(source, string.Empty);
        }

        public static string RemoveIfEndsWith(this string source, string stringToRemove)
        {
            if (source.EndsWith(stringToRemove))
                source = source.Substring(0, source.Length - stringToRemove.Length);

            return source;
        }

    }

    public class Utility
    { 
        public static RepositoryItemGridLookUpEdit GetGLESablon(DataTable SourceTable, string DisplayMember, string ValueMember)
        {
            RepositoryItemGridLookUpEdit gleSablon = new RepositoryItemGridLookUpEdit();
            gleSablon.AllowNullInput = DefaultBoolean.True;
            gleSablon.View.OptionsView.ColumnAutoWidth = false;
            gleSablon.View.OptionsView.ShowAutoFilterRow = true;
            gleSablon.View.OptionsBehavior.Editable = false;
            gleSablon.View.BestFitMaxRowCount = 50;
            gleSablon.View.OptionsFind.HighlightFindResults = true;
            gleSablon.View.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;
            gleSablon.NullText = string.Empty;
            gleSablon.ImmediatePopup = true;

            gleSablon.PopupFormMinSize = new Size(500, 500);
            gleSablon.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFit;

            gleSablon.Popup += gleSablon_Popup;

            gleSablon.DataSource = SourceTable;
            gleSablon.DisplayMember = DisplayMember;
            gleSablon.ValueMember = ValueMember;

            gleSablon.PopulateViewColumns();

            foreach (GridColumn gc in gleSablon.View.Columns)
            {
                gc.FilterMode = ColumnFilterMode.DisplayText;


            }

            return gleSablon;
        }

        static void gleSablon_Popup(object sender, EventArgs e)
        {
            GridLookUpEdit gle = (GridLookUpEdit)sender;
            gle.Properties.View.BestFitColumns();
            gle.Popup -= gleSablon_Popup;
        }

        public static RepositoryItemGridLookUpEdit GetAutoCompGLE(DataTable dt, string displayMember,string valueMember)
        {
            return Utility.GetGLESablon(dt, displayMember, valueMember);
        }
    }
}